﻿// Copyright 2008 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
// 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BTGISWASECO.DataLayer;

public class IdentifyTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
{
    #region IMapServerToolAction Members

    void ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction(
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs)
    {
        // Get reference to the map control
        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap =
            (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;
        try
        {
            // Cast tool event arguments to map point event arguments, which allows
            // easy access to the map point clicked by the user
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs mapPointEventArgs =
                (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs)toolEventArgs;
            // Get the map point from the map point event arguments
            ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint = mapPointEventArgs.MapPoint;

            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mapFunctionality =
                adfMap.GetFunctionality("BTGISWASECO");

            // Retrieve the resource for the current map functionality object
            ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource = mapFunctionality.Resource;

            // Check whether the current resource supports querying
            bool supportsQuery = gisResource.SupportsFunctionality(typeof
                (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));

            if (supportsQuery)
            {
                // Create a query functionality object from the current resource
                ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality =
                    (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
                    gisResource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

                // Get the names and ids of the queryable layers in the current resource
                string[] layerIDs;
                string[] layerNames;
                queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames);

                // Set the layer name on which you want to identify
                string activeLayerName = "Đồng hồ khách hàng";

                // Iterate through the layer names until a match is found for
                // activeLayerName.  Include a call to ToUpper() or ToLower() in the comparison
                // to eliminate case sensitivity.
                string activeLayerID = null;
                for (int index = 0; index < layerNames.Length; index++)
                {
                    if (layerNames[index].ToUpper() == activeLayerName.ToUpper())
                    {
                        // Get the layer id of the match found from the layer id array
                        activeLayerID = layerIDs[index];
                        break;
                    }
                }

                // Initialize a variable to store tolerance.  This will be used as the tolerance
                // around the point clicked by the user
                int pixelTolerance = 3;

                // Execute the identify operation
                System.Data.DataTable[] resultDataTableArray = queryFunctionality.Identify(
                    mapFunctionality.Name, adfPoint, pixelTolerance,
                    ESRI.ArcGIS.ADF.Web.IdentifyOption.AllLayers, new string[] { activeLayerID });
                DataAccessHelper db = new DataAccessHelper();
                db.DataTableLayer = resultDataTableArray[0];
                db.LayerNames = activeLayerName;
                HttpContext.Current.Session["DHKhachHang"] = resultDataTableArray[0];
                HttpContext.Current.Response.Redirect("PresentationLayer/DongHoKhachHang.aspx");
                // Exit the loop if no results were found
                if (resultDataTableArray == null)
                    return;
                // Display results in a GridView
                Utility.DisplayOrHideSelectionTable(adfMap, 
                    (ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer)resultDataTableArray[0], true);                
            }
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorAlertCallbackResult =
                Utility.CreateErrorCallbackResult(exception);
            adfMap.CallbackResults.Add(errorAlertCallbackResult);
        }

    }
    #endregion
}


